<?php
require_once ("secure_area.php");
class Payment extends Secure_area
{
	function __construct()
	{
		parent::__construct('payment');
	}
	
	function index()
	{
           
		$config['base_url'] = site_url('payment/index');
		$config['total_rows'] = $this->Payments->count_all();
		$config['per_page'] = $this->config->item('number_of_items_per_page') ? (int)$this->config->item('number_of_items_per_page') : 20; 
		$this->pagination->initialize($config);
		
		$data['controller_name']=strtolower(get_class());
		$data['form_width']=$this->get_form_width();
		$data['manage_table']=get_payment_manage_table($this->Payments->get_all($config['per_page'], $this->uri->segment(3)),$this);
		$this->load->view('payment/manage',$data);
	}
	
	/*
	Returns payment table data rows. This will be called with AJAX.
	*/
	function search()
	{
		$search=$this->input->post('search');
		$data_rows=get_payment_manage_table_data_rows($this->Payments->search($search,$this->config->item('number_of_items_per_page') ? (int)$this->config->item('number_of_items_per_page') : 20),$this);
		echo $data_rows;
	}
	
	/*
	Gives search suggestions based on what is being searched for
	*/
	function suggest()
	{
		$suggestions = $this->payment->get_search_suggestions($this->input->get('term'),100);
		echo json_encode($suggestions);
	}
	
	/*
	Loads the payment edit form
	*/
	function view($payment_id=-1)
	{
		$data['payment']=$this->Payments->get_info($payment_id);
                
		$this->load->view("payment/form",$data);
	}
	
	/*
	Inserts/updates a payment
	*/
	function save($payment_id=-1)
	{
		$payment_data = array(
                    'name'=>$this->input->post('name')		
		);
		
                
		
		if($this->Payments->save($payment_data,$payment_id))
		{
			
			//New payment
			if($payment == -1)
			{
				echo json_encode(array('success'=>true,'message'=>lang('payment_successful_adding').' '.
				$payment_data['name'],'payment_id'=>$payment_data['payment_id']));
			}
			else //previous payment
			{
				echo json_encode(array('success'=>true,'message'=>lang('payment_successful_updating').' '.
				$person_data['name'],'payment_id'=>$payment_id));
			}
		}
		else//failure
		{	
			echo json_encode(array('success'=>false,'message'=>lang('payment_error_adding_updating').' '.
			$person_data['name'] ,'payment_id'=>-1));
		}
	}
	
	/*
	This deletes payment from the payment table
	*/
	function delete()
	{
		$payment_to_delete=$this->input->post('ids');
		
		if($this->Payments->delete_list($payment_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>lang('payment_successful_deleted').' '.
			count($payment_to_delete).' '.lang('payment_one_or_multiple')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>lang('payment_cannot_be_deleted')));
		}
	}
	
	function excel()
	{
		$data = file_get_contents("import_customers.csv");
		$name = 'import_customers.csv';
		force_download($name, $data);
	}
	
	function excel_import()
	{
		$this->load->view("payment/excel_import", null);
	}

	function do_excel_import()
	{
		$msg = 'do_excel_import';
		$failCodes = array();
		if ($_FILES['file_path']['error']!=UPLOAD_ERR_OK)
		{
			$msg = lang('items_excel_import_failed');
			echo json_encode( array('success'=>false,'message'=>$msg) );
			return;
		}
		else
		{
			if (($handle = fopen($_FILES['file_path']['tmp_name'], "r")) !== FALSE)
			{
				//Skip first row
				fgetcsv($handle);
				
				$i=1;
				while (($data = fgetcsv($handle)) !== FALSE) 
				{
					$person_data = array(
					'first_name'=>$data[0],
					'last_name'=>$data[1],
					'email'=>$data[2],
					'phone_number'=>$data[3],
					'address_1'=>$data[4],
					//'address_2'=>$data[5],
					//'city'=>$data[5],
					'state'=>$data[5],
					'suburd'=>$data[6],
					'zip'=>$data[7]
					//'country'=>$data[9],
					//'comments'=>$data[10]
					);
					/*
					$customer_data=array(
					'account_number'=>$data[11]=='' ? null:$data[11],
					'taxable'=>$data[12]=='' ? 0:1,
					'company_name' => $data[13],
					);
					*/
					if(!$this->payment->save($person_data,$customer_data))
					{	
						$failCodes[] = $i;
					}
					
					$i++;
				}
			}
			else 
			{
				echo json_encode( array('success'=>false,'message'=>lang('common_upload_file_not_supported_format')));
				return;
			}
		}

		$success = true;
		if(count($failCodes) > 1)
		{
			$msg = lang('customers_most_imported_some_failed')." (" .count($failCodes) ."): ".implode(", ", $failCodes);
			$success = false;
		}
		else
		{
			$msg = lang('customers_import_successfull');
		}

		echo json_encode( array('success'=>$success,'message'=>$msg) );
	}
	
	/*
	get the width for the add/edit form
	*/
	function get_form_width()
	{			
		return 550;
	}
}
?>